package com.jzr.medical.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.medical.db1.model.TRole;
import com.jzr.medical.db1.service.RoleService;
import com.jzr.medical.db1.vo.RoleDeptVo;
import com.jzr.medical.db1.vo.RoleMenuTreeVo;
import com.jzr.medical.db1.vo.RoleUserVo;
import com.jzr.medical.db1.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public Result<List<RoleVo>> list(RoleVo record){
        QueryWrapper<RoleVo> qw = new QueryWrapper<>();
        qw.likeRight(U.isNotBlank(record.getCode()), RoleVo.Code_,record.getCode())
                .likeRight(U.isNotBlank(record.getName()), RoleVo.Name_,record.getName());
        List<RoleVo> list = roleService.list(qw);
        return Result.success("成功",list);
    }

    @PostMapping("/add")
    public Result<RoleVo> add(RoleVo record){
        if(record == null){
            return Result.fail("参数不能为空");
        }
        record.setStatus(1);
        RoleVo rt = roleService.saveAndGet(record);
        return Result.success("成功",rt);
    }

    @GetMapping("/delete")
    public Result<Boolean> delete(TRole record){
        if(roleService.isHasMenu(record.getId())){
            return Result.fail("存在菜单，不能删除");
        }
        if(roleService.isHasUser(record.getId())){
            return Result.fail("存在用户，不能删除");
        }
        Boolean rt = roleService.removeById(record.getId());
        return Result.success("成功",rt);
    }

    @PostMapping("/update")
    public Result<RoleVo> update(RoleVo record){
        RoleVo rt = roleService.updateAndGet(record);
        return Result.success("成功",rt);
    }

    @PostMapping("/save")
    public Result<RoleVo> save(RoleVo record){
        if(!U.isBlank(record.getId())){
            return this.update(record);
        }else{
            return this.add(record);
        }
    }

    @GetMapping("/codeList")
    public Result<List<Map<String,Object>>> codeList(){
        List<Map<String,Object>> list = roleService.codeList();
        return Result.success("成功",list);
    }

    @PostMapping("/saveBatch")
    public Result<List<RoleVo>> saveBatch(@RequestBody List<RoleVo> list){
        List<RoleVo>  result = roleService.saveBatch(list);
        return Result.success("成功",result);
    }

    @GetMapping("/getRoleMenuTree")
    public Result<List<RoleMenuTreeVo>> getRoleMenuTree(Integer roleId){
        List<RoleMenuTreeVo> list = roleService.getRoleMenuTree(roleId);
        return Result.success("成功",list);
    }

    @PostMapping("/saveRoleMenuTree")
    public Result<Boolean> saveRoleMenuTree(Integer roleId, String menuIds){
        if(U.isBlank(roleId)){
            return Result.fail("角色roleId不能为空");
        }
        if(U.isBlank(menuIds)){
            return Result.fail("菜单项目不能为空");
        }
        List<Integer> menuIdList = new ArrayList<>();
        String[] menuIdArray = menuIds.split(",");
        for(String s : menuIdArray){
            if(!s.isEmpty()) {
                menuIdList.add(Integer.parseInt(s));
            }
        }
        roleService.saveRoleMenuTree(roleId,menuIdList);
        return Result.success("成功",true);
    }

    @GetMapping("/getUserList")
    public Result<List<RoleUserVo>> getUserList(Integer roleId){
        List<RoleUserVo> list = roleService.getUserList(roleId);
        return Result.success("成功",list,list.size());
    }

    @PostMapping("/saveUserList")
    public Result<Boolean> saveUserList(Integer roleId, String userIds){
        if(U.isBlank(roleId)){
            return Result.fail("角色roleId不能为空");
        }
        if(U.isBlank(userIds)){
            return Result.fail("用户项目不能为空");
        }
        List<Integer> userIdList = new ArrayList<>();
        String[] userIdArray = userIds.split(",");
        for(String s : userIdArray){
            if(!s.isEmpty()) {
                userIdList.add(Integer.parseInt(s));
            }
        }
        roleService.saveUserList(roleId,userIdList);
        return Result.success("成功",true);
    }

    @GetMapping("/getDeptList")
    public Result<List<RoleDeptVo>> getDeptList(Integer roleId){
        List<RoleDeptVo> list = roleService.getDeptList(roleId);
        return Result.success("成功",list,list.size());
    }

    @PostMapping("/saveDeptList")
    public Result<Boolean> saveDeptList(Integer roleId, String deptIds){
        if(U.isBlank(roleId)){
            return Result.fail("角色roleId不能为空");
        }
        if(U.isBlank(deptIds)){
            return Result.fail("科室列表不能为空");
        }
        List<Integer> deptIdList = new ArrayList<>();
        String[] userIdArray = deptIds.split(",");
        for(String s : userIdArray){
            if(!s.isEmpty()) {
                deptIdList.add(Integer.parseInt(s));
            }
        }
        roleService.saveDeptList(roleId,deptIdList);
        return Result.success("成功",true);
    }
}
